﻿using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/// <remarks>
/// Note:
/// When testing, log files would be created in the Vsiaul Studio directory.
/// (such as C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE)
/// instead of the application directory.
/// </remarks>
namespace Logger
{
    /// <summary>
    /// File Logger class that write a log message into the today's log file
    /// </summary>
    /// <remarks>
    /// This class is thread safe. 
    /// </remarks>
    /// <dates>
    /// 15/05/2011 - created by Chiaki Sato
    /// </dates>
    public class FileLogger : ILogger
    {
        private const string LOG_FILE_NAME = "logger_{0}.log";
        private const string LOG_FILE_NAME_DATE_FORMAT = "yyyyMMdd";
        private const string DEFAULT_TRACE_LISTENER_NAME = "Default";
        private ILoggerMessage stackTraceMessage;
        private ILoggerMessage hostAddressMessage;
        private string logFile;
        private string logPath;
        public enum LogType { UNIT = 1, COMPO = 2 }

        /// <summary>
        /// Default constructor for FileLogger class.
        /// Log files will be created in this application directory as default. 
        /// </summary>
        public FileLogger(LogType logType)
            : this(string.Format(LOG_FILE_NAME, DateTime.Now.ToString(LOG_FILE_NAME_DATE_FORMAT))
                            , Directory.GetCurrentDirectory())
        {
            stackTraceMessage = new StackTraceMessage((StackTraceMessage.LogType)logType);
            hostAddressMessage = HostAddressSingleton.Instance;
        }

        /// <summary>
        /// Constructor for FileLogger class
        /// Log Files will be created with default filename in the directory specified by path
        /// </summary>
        /// <param name="path">Log file directory path</param>
        public FileLogger(LogType logType, string path)
            : this(string.Format(LOG_FILE_NAME, DateTime.Now.ToString(LOG_FILE_NAME_DATE_FORMAT))
                , path)
        {
            stackTraceMessage = new StackTraceMessage((StackTraceMessage.LogType)logType);
            hostAddressMessage = HostAddressSingleton.Instance;
        }
        /// <summary>
        /// Constructor for FileLogger class.
        /// Log files will be created in this application directory as default. 
        /// </summary>
        /// <param name="logFileName">log file name</param>
        /// <param name="path">log file directory path</param>
        public FileLogger(string logFileName, string path)
        {
           // DefaultTraceListener dtl = (DefaultTraceListener)Debug.Listeners[DEFAULT_TRACE_LISTENER_NAME];
          //  dtl.LogFileName = Path.Combine(path, logFileName); setting debug path?
            logFile = logFileName;
            logPath = Path.Combine(path, logFileName);
        }

        /// <summary>
        /// Write a log message with stack trace into a file
        /// </summary>
        /// <param name="message"></param>
        /// <remarks>
        /// From MSDN: Debug class is thread safe. Thus, Debug.WriteLine() is also thread safe. 
        /// </remarks>
        public string Write(string message)
        {
            TextWriter writer;
            string logMessage = hostAddressMessage.CreateLogMessage()
                              + stackTraceMessage.CreateLogMessage()
                              + message;
            //Debug.WriteLine(logMessage);  trying to write with debug??

            if (!File.Exists(logPath))
            {
                writer = new StreamWriter(logPath);
            }
            else
            {
                writer = File.AppendText(logPath);
            }
            writer.WriteLine(logMessage);
            writer.Close();

            return (logMessage);
        }
    }
}
